TARGET = libstm32f030xc.a
BUILD_DIR = build
OUTPUT_DIR = output
OPT = -Og
DEBUG = 0
PREFIX = arm-none-eabi-
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
SZ = $(PREFIX)size
CP = $(PREFIX)objcopy
AR = $(PREFIX)ar
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
CPU = -mcpu=cortex-m0
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
C_DEFS =  \
-DSTM32F030xC \
-DUSE_HAL_DRIVER \
-DHSE_VALUE=25000000

ASM_SOURCES = \
CMSIS/Device/ST/STM32F0xx/Source/Templates/gcc/startup_stm32f030xc.s

C_SOURCES = \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_adc.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_adc_ex.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_can.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_cec.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_comp.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_cortex.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_crc.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_crc_ex.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_dac.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_dac_ex.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_dma.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_flash.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_flash_ex.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_gpio.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_i2c.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_i2c_ex.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_i2s.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_irda.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_iwdg.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_pcd.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_pcd_ex.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_pwr.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_pwr_ex.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_rcc.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_rcc_ex.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_rtc.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_rtc_ex.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_smartcard.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_smartcard_ex.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_smbus.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_spi.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_spi_ex.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_tim.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_tim_ex.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_tsc.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_uart.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_uart_ex.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_usart.c \
STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_wwdg.c \
CMSIS/Device/ST/STM32F0xx/Source/Templates/system_stm32f0xx.c

C_INCLUDES = \
-ICMSIS/Include \
-ICMSIS/Device/ST/STM32F0xx/Include \
-ISTM32F0xx_HAL_Driver/Inc

ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -std=c99
ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"

all: $(TARGET)

OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))

$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 
	@$(info $(notdir $@))
	@$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@

$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
	@$(info $(notdir $@))
	@$(AS) -c $(ASFLAGS) $< -o $@

$(TARGET): $(OBJECTS) Makefile
	@$(info $(notdir $@))
	@$(AR) -r $@ $(OBJECTS)
	@rm -f ../Lib/$@ && mkdir -p ../Lib && cp $@ ../Lib

$(BUILD_DIR):
	@mkdir -p $@

clean:
	@rm -rf $(BUILD_DIR)
	@rm -f $(TARGET)

-include $(wildcard $(BUILD_DIR)/*.d)
